import moment from 'moment'

Component({
  options: {
    addGlobalClass: true,
  },

  properties: {
    id: {
      type: String,
      value: '',
      observer(id) {
        if (this.properties.thresholds?.length) {
          this.createIntersectionObserverHandle();
        }
      },
    },
    data: {
      type: Object,
      observer(data) {
        if (!data) {
          return;
        }
        // console.log("a")
        if (data.carImages != null && data.carImages.length > 0) {
          for (var i = 0; i < data.carImages.length; i++) {
            data.carImages[i] = getApp().globalData.imageUrl + data.carImages[i];
          }
          data.image = data.carImages[0];
        }
        data.title = data.auctionName;
        data.type = data.auctionType;
        data.status = data.auctionFlag;
        //按开始时间重置拍卖状态
        if (moment().isBefore(data.config.startTime)) {
          data.status = 0;
        }
        // if(moment(details.config.endTime).isBefore(moment())){
        //   details.status = 2;
        // }


        // data.residueSecond = moment(data.config.endTime)
        //   .diff(moment(),
        //     "seconds")
        // console.log(hours, details.residueSecond)
        data.startTime = moment(data.config.startTime).format("MM月DD日HH:mm");
        data.endTime = moment(data.config.endTime).format("MM月DD日HH:mm");

        // if (data.status == 1) {
        //   data.config.startTime = moment(data.config.startTime).format("MM月DD日HH:mm");
        // } else if (data.status == 2) {
        //   data.config.endTime = moment(data.config.endTime).format("MM月DD日HH:mm");
        // }
        this.setData({ goods: data });
      },
    },
    currency: {
      type: String,
      value: '¥',
    },

    thresholds: {
      type: Array,
      value: [],
      observer(thresholds) {
        if (thresholds && thresholds.length) {
          this.createIntersectionObserverHandle();
        } else {
          this.clearIntersectionObserverHandle();
        }
      },
    },
  },

  data: {
    goods: { id: '' }
  },

  lifetimes: {
    ready() {
      this.init();
    },
    detached() {
      this.clear();
    },
  },

  pageLifeTimes: {},

  methods: {
    clickHandle() {
      this.triggerEvent('click', { goods: this.data.goods });
    },


    init() {
      const { thresholds, id } = this.properties;
      if (thresholds && thresholds.length) {
        this.createIntersectionObserverHandle();
      }
    },

    clear() {
      this.clearIntersectionObserverHandle();
    },

    intersectionObserverContext: null,


    clearIntersectionObserverHandle() {
      if (this.intersectionObserverContext) {
        try {
          this.intersectionObserverContext.disconnect();
        } catch (e) { }
        this.intersectionObserverContext = null;
      }
    },
  },
});
